package com.bsdk.install.device.watermeter.v1.cmd;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bsdk.install.device.constants.LockConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class ANALYSIS  extends Cmd{

    private JSONObject reData = new JSONObject();


    /***
     * @description
     * @author
     * @throws
     * @return com.alibaba.fastjson2.JSONObject
     * @time 16:51
     */
    public JSONObject dealJsonData(JSONObject jsonObject) {

        int i, j = 0, k;
        String str_data;
        String tmp;
        int len;
        byte[] hex_data = new byte[200];

        if(jsonObject == null){

            reData.put("code", STATUS_ERROR);
            return reData;
        }
        str_data = (String) jsonObject.get("data");
        len = str_data.length();
        if (len == 0) {
            reData.put("code", STATUS_ERROR);
            return reData;
        }
        log.info("str_data：" + str_data);
        len = len / 2;
        //写一个转换函数
        for (i = 0; i < len; i++) {
            k = 2 * i;
            tmp = str_data.substring(k, (k + 2));
            k = Integer.parseInt(tmp, 16);
            hex_data[j++] = (byte) k;

        }
        log.info("16:", Arrays.toString(hex_data));
        log.info("str_data：" + str_data);
        JSONObject retJsonObject;
        //先检测模块的协议
        retJsonObject = Moudle_Packet_analysis(hex_data, j);
        return retJsonObject;
       }



    public JSONObject Moudle_Packet_analysis(byte[] m_data, int len) {
        JSONObject jsonObject = new JSONObject();
        int m_rev_state = MOUDLE_CMD_RECEIVE_HEAD;
        int data_total_len = 0;
        int rcv_len = 0;
        byte rcv_char;
        byte[] m_packet_data = new byte[100];
        int j = 0, i;
        if (len < 1) return jsonObject;

        while (len != j) {
            rcv_char = m_data[j++];
            switch (m_rev_state) {
                case MOUDLE_CMD_RECEIVE_HEAD:
                    if (rcv_char == 0x02) m_rev_state = MOUDLE_CMD_RECEIVE_LEN1;
                    rcv_len = 0;
                    m_packet_data[rcv_len++] = rcv_char;
                    break;
                case MOUDLE_CMD_RECEIVE_LEN1://
                    m_packet_data[rcv_len++] = rcv_char;
                    data_total_len = rcv_char;
                    m_rev_state = MOUDLE_CMD_RECEIVE_LEN2;
                    break;
                case MOUDLE_CMD_RECEIVE_LEN2://
                    m_packet_data[rcv_len++] = rcv_char;
                    data_total_len = (rcv_char*0x100) + data_total_len + 3;
                    m_rev_state = MOUDLE_CMD_RECEIVE_CMD_DATA;
                    break;
                case MOUDLE_CMD_RECEIVE_CMD_DATA:
                    m_packet_data[rcv_len++] = rcv_char;
                    if (rcv_len == data_total_len) m_rev_state = MOUDLE_CMD_RECEIVE_CRC;
                    break;
                case MOUDLE_CMD_RECEIVE_CRC: //异或
                    if (rcv_char == check_crc(m_packet_data, rcv_len)) {
                        m_rev_state = MOUDLE_CMD_RECEIVE_TAIL;
                        m_packet_data[rcv_len++] = rcv_char;

                    } else{
                        m_packet_data[rcv_len++] = rcv_char;
                        m_rev_state = MOUDLE_CMD_RECEIVE_TAIL;
                    }

                    break;
                case MOUDLE_CMD_RECEIVE_TAIL:
                    if (rcv_char == (byte)0x0FF) {
                        m_packet_data[rcv_len++] = rcv_char;
                        m_rev_state = MOUDLE_CMD_RECEIVE_OK;
                    } else {
                        m_rev_state = MOUDLE_CMD_RECEIVE_HEAD;
                    }
                    break;

                default:
                    break;
            }
            if (MOUDLE_CMD_RECEIVE_OK == m_rev_state) {
                jsonObject = moudl_packet_process(m_packet_data, rcv_len);
                m_rev_state = MOUDLE_CMD_RECEIVE_HEAD;
            }
        }
        return jsonObject;
    }
    public JSONObject moudl_packet_process(byte[] buf, int len) {
        JSONObject jsonObject = new JSONObject();
        int i,j,m_len = 0;
        byte m_CMD;
        byte[] array =new byte[100];
        StringBuilder rcv_data = new StringBuilder();
            for(i = 0;i< len;i++) {
            rcv_data.append(String.format("%02X", buf[i]));
        }
        JSONObject jsonData = new JSONObject();
        StringBuilder tmp_data = new StringBuilder();
        //02010077ECFF
        reData.put("code", STATUS_SUCCESS);
        m_CMD = buf[3];
        if(m_CMD>0x80)m_CMD = (byte)(m_CMD-0x80);//返回的信息
        switch (m_CMD) {
            case GW_CMD_MQTT_SET://MQTT参数设置
                reData.put("code",GW_CMD_MQTT_SET);
                break;
            case GW_CMD_MQTT_REQ://MQTT参数设置查询
                reData.put("code",GW_CMD_MQTT_REQ);
                j = 4;
                //devid
                m_len = buf[j++];
                for(i = 0;i<m_len;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("devid",array.toString());
                //user
                m_len = buf[j++];
                for(i = 0;i<m_len;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("user",array.toString());
                //pwd
                m_len = buf[j++];
                for(i = 0;i<m_len;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("pwd",array.toString());
                //IP
                m_len = buf[j++];
                for(i = 0;i<m_len;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("Ip",array.toString());
                reData.put("Port",""+(buf[j++]+(buf[j++]*0x100)));
                break;
            case  GW_CMD_DOWN_START://FTP开始升级
                reData.put("code",GW_CMD_DOWN_START);
                break;
            case  GW_CMD_RESET_GW://复位设备
                reData.put("code",GW_CMD_RESET_GW);
                break;
            case  GW_CMD_TIME_SET://网关时间设置
                reData.put("code",GW_CMD_TIME_SET);
                break;
            case  GW_CMD_TIME_REQ://网关时间设置查询
                reData.put("code",GW_CMD_TIME_REQ);
                j = 4;
                //reData.put("time",""+(buf[j++]*0x100+buf[j++]) +"-"+ buf[j++]+"-"+ buf[j++]+"-"+ buf[j++]+"-"+ buf[j++]+"-"+ buf[j++]);
                break;
            case  GW_CMD_DEVID_SET://网关devid的设置
                reData.put("code",GW_CMD_DEVID_SET);
                break;
            case  GW_CMD_DEVID_REQ://网关devid的设置查询
                reData.put("code",GW_CMD_DEVID_REQ);
               j = 4;
                //ID
                m_len = buf[j++];
                for(i = 0;i<m_len;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("id",array.toString());

                //ApiKey
                m_len = buf[j++];
                for(i = 0;i<m_len;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("apikey",array.toString());
                //chipid
                m_len = buf[j++];
                for(i = 0;i<m_len;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("chipid",array.toString());
                break;
            case  GW_CMD_INFO_REQ://网关硬件信息查询
                reData.put("code",GW_CMD_INFO_REQ);
                j = 4;
                for(i = 0;i<len-2;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("Version",array.toString());
                break;
            case  GW_CMD_AUTH_IP_PORT_SET://AUTH IP PORT
                reData.put("code",GW_CMD_AUTH_IP_PORT_SET);
                break;
            case  GW_CMD_AUTH_IP_PORT_REQ://AUTH IP PORT
                reData.put("code",GW_CMD_AUTH_IP_PORT_REQ);
                j = 4;
                for(i = 0;i<len-2;i++) array[i] = buf[j++];array[i] = 0;
                reData.put("auth",array.toString());
                break;
            default:
                break;

        }
        reData.put("data",jsonObject);
        //reData.put("Raw_data",rcv_data);
        return reData;

    }




}